package com.ms.qw.jsf.aop;

import com.ms.qw.jsf.util.FindCrudBeanBase;
import com.ms.qw.server.infrastructure.persistence.exception.ApplicationException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.primefaces.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Aspect
@Component("jsfBeanAspect")
public class JsfBeanAspect {
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @PostConstruct
    public void init(){

    }

    @Pointcut("bean(*JsfBean)")
    private void jsfBean() {

    }

    @Pointcut("execution(public * *(javax.faces.event.ActionEvent))")
    private void publicMethodActionEvent() {
    }
    @Pointcut("execution(public * *(org.primefaces.event.FileUploadEvent))")
    private void publicMethodFileUploadEvent() {
    }
    @Pointcut("execution(public * *(org.primefaces.event.SelectEvent))")
    private void publicMethodSelectEvent() {
    }

    @Around("jsfBean() && (publicMethodActionEvent() || publicMethodFileUploadEvent() || publicMethodSelectEvent() )")
    public Object logAllMethodsPainterAndTeacher(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        String method =proceedingJoinPoint.getSignature().toShortString();
        try {
            logger.info("Around: Before executing:" + method);
            Object obj = proceedingJoinPoint.proceed();
            logger.info("Around: After executing:" + method);
            return obj;
        } catch (Throwable throwable) {
            logger.error("Problemas llamado al metodo: "+method,throwable);
            Object target = proceedingJoinPoint.getTarget();
            if (target instanceof FindCrudBeanBase) {
                FindCrudBeanBase find = (FindCrudBeanBase) target;
                if(throwable instanceof ApplicationException){
                    find.addMessage(throwable.getMessage(), FacesMessage.SEVERITY_FATAL, ((ApplicationException) throwable).getParameters());
                }else{
                    if(throwable.getMessage()==null){
                        find.addMessage("Error en el sistema.", FacesMessage.SEVERITY_FATAL, throwable);
                    }else{
                        find.addMessage(throwable.getMessage(), FacesMessage.SEVERITY_FATAL, throwable);
                    }

                }

            }
            RequestContext context = RequestContext.getCurrentInstance();
            context.addCallbackParam("existError", true);
            return null;
        }
    }

    public static void main(String[] args) {
//        R1S1XXXRA
        String pattern = "\\[^R1S1]([RA])$";
        Pattern p = Pattern.compile("R1S1([0-9]*)RA");

        System.out.println(p.matcher("R1S1XXXRA").find());
        System.out.println(p.matcher("R1S1212RA").find());
        System.out.println(p.matcher("R1S12RA").find());

    }

}